package top.hzy520.message.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.transaction.annotation.Transactional;
import top.hzy520.message.dao.MessageEventProcessMapper;
import top.hzy520.message.entity.MessageEventProcess;

/**
 * @Author: HouZeYu
 * @Description:  消息接收接口
 * @Date: Created in 20:50 2018/7/19
 */
@EnableBinding(Sink.class)
public class MessageReceiveService {

    private static final Byte MESSAGE_TYPE_POINTS = 1; // 积分消息类型

    private static final Byte MESSAGE_UNCONSUMPTIONED = 0; //消息没有被处理

    @Autowired
    private MessageEventProcessMapper messageEventProcessMapper;
    /**
     *  收到消息并处理
     * @param payload 发送的消息
     */
    @StreamListener(Sink.INPUT)
    @Transactional
    public void receiveMessage(Object payload){
   MessageEventProcess messageEventProcess=new MessageEventProcess();
   messageEventProcess.setStatus(MESSAGE_UNCONSUMPTIONED);
   messageEventProcess.setType(MESSAGE_TYPE_POINTS);
   messageEventProcess.setPayload((String) payload);
        // 收到消息 插入接收消息表
           messageEventProcessMapper.insertSelective(messageEventProcess);
    }
}
